package SchoolController;

import com.alibaba.fastjson.JSONObject;
import com.renjun.smweb.commons.util.Encrypt;
import com.renjun.smweb.controller.SchoolController.SchoolController;
import com.renjun.smweb.dao.pojo.LoginInfo;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lrs on 2017/2/23.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath*:/spring-mvc.xml", "classpath*:/spring-mybatis.xml"})
public class SchoolControllerTestCase {


    //模拟request,response
    private MockHttpServletRequest request;
    private static MockHttpServletResponse response;

    @Autowired
    private SchoolController schoolController;

    @Before
    public void setUp() throws Exception {
        request = new MockHttpServletRequest();
        request.getSession().setAttribute("userinfo", getLoginfo());
//        response = new MockHttpServletResponse();
    }

    @Test
    public void testAddSchool() {
        System.out.println("添加学校/新增学校");
        Map<String, Object> params = new HashMap<>();
        //组装参数
        validateSchoolCode3(params); //测试学校代码是否为空
//        validateSchoolCode1(params); //测试学校代码长度
//        validateSchoolCode2(params); //测试学校代码是否纯数字
//        validateOrgid2(params); //测试组织机构 是否为空
//        validateOrgid1(params); //测试组织结构 非(大写英文字母、数字、英文中划线)
//        validateSchoolYwName2(params); //测试学校英文名称是否为空
//        validateSchoolYwName1(params); //测试学校英文名称 非(大小写英文字母、间隔符号、空格、英文中划线
//        validateSchoolTel(params); //测试学校电话号码是否为数字
//        validateFaxNum(params); //测试学校传真是否为(数字、英文中划线)
//        validateZipCode1(params); //测试邮政编码长度是否为6位
//        validateZipCode2(params); //邮政编码是否为数字
//        validateAdminField(params); //测试学校行政字段是否有空
//        validtePreTel(params); //测试校长手机号码是否是数字
//        validateDistrictCode(params); //测试区县代码是否为空
//        validateXxbb(params); //测试学校办别和办学等级对应关系

        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //添加学校
        JSONObject rs = schoolController.addSchool(request, response);
        String jiem = (String) rs.get("response");
        System.out.println(Encrypt.jiem(jiem));
//        String str = JSON.toJSONString(rs);
//        System.out.println("\n json对象：" + str + "\n");
    }

    //测试学校代码长度
    private void validateSchoolCode1(Map<String, Object> params) {
        params.put("schoolcode", "0002");
        params.put("schoolname", "金山好好学习幼儿园");
        params.put("address", "金山区");
        params.put("zipcode", "200000");
        params.put("orgid", "1234");
        params.put("faxnum", "031-1234");
        params.put("schooltelephone", "7711789");
        params.put("schoolurl", "http://www.baidu.com");
        params.put("districtcode", "310116");
        params.put("jxny", "20160606");
        params.put("xxlb", "0");
        params.put("xxbb", "3");
        params.put("bxdj", "5");
        params.put("prename", "陈校长");
        params.put("pretel", "你猜");
        params.put("schooljz", "16103");
    }

    //测试学校代码是否纯数字
    private void validateSchoolCode2(Map<String, Object> params) {
        params.put("schoolcode", "0002第三方");
        params.put("schoolname", "金山好好学习幼儿园");
        params.put("address", "金山区");
        params.put("zipcode", "200000");
        params.put("orgid", "1234");
        params.put("faxnum", "031-1234");
        params.put("schooltelephone", "7711789");
        params.put("schoolurl", "http://www.baidu.com");
        params.put("districtcode", "310116");
        params.put("jxny", "20160606");
        params.put("xxlb", "0");
        params.put("xxbb", "3");
        params.put("bxdj", "5");
        params.put("prename", "陈校长");
        params.put("pretel", "你猜");
        params.put("schooljz", "16103");
    }

    //测试学校代码是否为空
    private void validateSchoolCode3(Map<String, Object> params) {
        params.put("schoolcode", "");
        params.put("schoolname", "金山好好学习幼儿园");
        params.put("address", "金山区");
        params.put("zipcode", "200000");
        params.put("orgid", "1234");
        params.put("faxnum", "031-1234");
        params.put("schooltelephone", "7711789");
        params.put("schoolurl", "http://www.baidu.com");
        params.put("schoolywname", "werwer");
        params.put("districtcode", "310116");
        params.put("jxny", "20160606");
        params.put("xxlb", "0");
        params.put("xxbb", "3");
        params.put("bxdj", "5");
        params.put("sfytb", "0");
        params.put("prename", "陈校长");
        params.put("pretel", "212346546");
        params.put("schooljz", "16103");
    }

    //测试组织结构 非(大写英文字母、数字、英文中划线)
    private void validateOrgid1(Map<String, Object> params) {
        params.put("schoolcode", "3101160001");
        params.put("schoolname", "金山好好学习幼儿园");
        params.put("address", "金山区");
        params.put("zipcode", "200000");
        params.put("orgid", "1234SFS斯蒂芬");
        params.put("faxnum", "031-1234");
        params.put("schooltelephone", "7711789");
        params.put("schoolurl", "http://www.baidu.com");
        params.put("districtcode", "310116");
        params.put("jxny", "20160606");
        params.put("xxlb", "0");
        params.put("xxbb", "3");
        params.put("bxdj", "5");
        params.put("prename", "陈校长");
        params.put("pretel", "你猜");
        params.put("schooljz", "16103");
    }

    //测试组织机构是否为空
    private void validateOrgid2(Map<String, Object> params) {
        params.put("schoolcode", "3101160001");
        params.put("schoolname", "金山好好学习幼儿园");
        params.put("address", "金山区");
        params.put("zipcode", "200000");
        params.put("orgid", "");
        params.put("faxnum", "031-1234");
        params.put("schooltelephone", "7711789");
        params.put("schoolurl", "http://www.baidu.com");
        params.put("districtcode", "310116");
        params.put("jxny", "20160606");
        params.put("xxlb", "0");
        params.put("xxbb", "3");
        params.put("bxdj", "5");
        params.put("prename", "陈校长");
        params.put("pretel", "你猜");
        params.put("schooljz", "16103");
    }

    //测试学校英文名称 非(大小写英文字母、间隔符号、空格、英文中划线
    private void validateSchoolYwName1(Map<String, Object> params) {
        params.put("schoolcode", "3101160005");
        params.put("schoolname", "金山好好学习幼儿园");
        params.put("address", "金山区");
        params.put("zipcode", "200000");
        params.put("schoolywname", "jinshan金山");
        params.put("orgid", "324234");
        params.put("faxnum", "031-1234");
        params.put("schooltelephone", "7711789");
        params.put("schoolurl", "http://www.baidu.com");
        params.put("districtcode", "310116");
        params.put("jxny", "20160606");
        params.put("xxlb", "0");
        params.put("xxbb", "3");
        params.put("bxdj", "5");
        params.put("prename", "陈校长");
        params.put("pretel", "100000");
        params.put("sfytb", "0");
        params.put("schooljz", "16103");
    }

    //测试学校英文名称是否为空
    private void validateSchoolYwName2(Map<String, Object> params) {
        params.put("schoolcode", "3101160001");
        params.put("schoolname", "金山好好学习幼儿园");
        params.put("address", "金山区");
        params.put("zipcode", "200000");
        params.put("schoolywname", "");
        params.put("orgid", "2343242");
        params.put("faxnum", "031-1234");
        params.put("schooltelephone", "7711789");
        params.put("schoolurl", "http://www.baidu.com");
        params.put("districtcode", "310116");
        params.put("jxny", "20160606");
        params.put("xxlb", "0");
        params.put("xxbb", "3");
        params.put("bxdj", "5");
        params.put("prename", "陈校长");
        params.put("pretel", "你猜");
        params.put("schooljz", "16103");
    }

    //测试学校电话号码是否为数字
    private void validateSchoolTel(Map<String, Object> params) {
        params.put("schoolcode", "3101160001");
        params.put("schoolname", "金山好好学习幼儿园");
        params.put("address", "金山区");
        params.put("zipcode", "200000");
        params.put("schoolywname", "ewrwer");
        params.put("orgid", "32423423");
        params.put("faxnum", "031-1234");
        params.put("schooltelephone", "7711789eee");
        params.put("schoolurl", "http://www.baidu.com");
        params.put("districtcode", "310116");
        params.put("jxny", "20160606");
        params.put("xxlb", "0");
        params.put("xxbb", "3");
        params.put("bxdj", "5");
        params.put("prename", "陈校长");
        params.put("pretel", "你猜");
        params.put("schooljz", "16103");
    }

    //测试学校传真是否为(数字、英文中划线)
    private void validateFaxNum(Map<String, Object> params) {
        params.put("schoolcode", "3101160001");
        params.put("schoolname", "金山好好学习幼儿园");
        params.put("address", "金山区");
        params.put("zipcode", "200000");
        params.put("schoolywname", "rer");
        params.put("orgid", "23423");
        params.put("faxnum", "031-1234aa");
        params.put("schooltelephone", "7711789");
        params.put("schoolurl", "http://www.baidu.com");
        params.put("districtcode", "310116");
        params.put("jxny", "201606");
        params.put("xxlb", "0");
        params.put("xxbb", "3");
        params.put("bxdj", "5");
        params.put("prename", "陈校长");
        params.put("pretel", "123213123");
        params.put("schooljz", "16103");
    }

    //测试邮政编码长度是否为6位
    private void validateZipCode1(Map<String, Object> params) {
        params.put("schoolcode", "3101160001");
        params.put("schoolname", "金山好好学习幼儿园");
        params.put("address", "金山区");
        params.put("zipcode", "200001010");
        params.put("schoolywname", "ere");
        params.put("orgid", "34234");
        params.put("faxnum", "031-1234");
        params.put("schooltelephone", "7711789");
        params.put("schoolurl", "http://www.baidu.com");
        params.put("districtcode", "310116");
        params.put("jxny", "201606");
        params.put("xxlb", "0");
        params.put("xxbb", "3");
        params.put("bxdj", "5");
        params.put("prename", "陈校长");
        params.put("pretel", "8978564");
        params.put("schooljz", "16103");
    }

    //测试邮政编码是否为数字
    private void validateZipCode2(Map<String, Object> params) {
        params.put("schoolcode", "3101160001");
        params.put("schoolname", "金山好好学习幼儿园");
        params.put("address", "金山区");
        params.put("zipcode", "200001010ee");
        params.put("schoolywname", "ewrwer");
        params.put("orgid", "324234");
        params.put("faxnum", "031-1234");
        params.put("schooltelephone", "7711789");
        params.put("schoolurl", "http://www.baidu.com");
        params.put("districtcode", "310116");
        params.put("jxny", "201606");
        params.put("xxlb", "0");
        params.put("xxbb", "3");
        params.put("bxdj", "5");
        params.put("prename", "陈校长");
        params.put("pretel", "234134");
        params.put("schooljz", "16103");
    }

    //测试学校行政字段是否有空
    private void validateAdminField(Map<String, Object> params) {
        params.put("schoolcode", "3101160001");
        params.put("schoolname", "金山好好学习幼儿园");
        params.put("address", "金山区");
        params.put("zipcode", "200001");
        params.put("schoolywname", "werwe");
        params.put("orgid", "23423423");
        params.put("faxnum", "031-1234");
        params.put("schooltelephone", "7711789");
        params.put("schoolurl", "http://www.baidu.com");
        params.put("districtcode", "310116");
        params.put("jxny", "20160606");
        params.put("xxlb", "");
        params.put("xxbb", "");
        params.put("bxdj", "");
        params.put("sfytb", "");
        params.put("prename", "陈校长");
        params.put("pretel", "435234534");
        params.put("schooljz", "16103");
    }

    //测试校长手机号码
    private void validtePreTel(Map<String, Object> params) {
        params.put("schoolcode", "3101160001");
        params.put("schoolname", "金山好好学习幼儿园");
        params.put("address", "金山区");
        params.put("zipcode", "200001");
        params.put("schoolywname", "ewrw");
        params.put("orgid", "234234");
        params.put("faxnum", "031-1234");
        params.put("schooltelephone", "7711789");
        params.put("schoolurl", "http://www.baidu.com");
        params.put("districtcode", "310116");
        params.put("jxny", "20160606");
        params.put("xxlb", "0");
        params.put("xxbb", "1");
        params.put("bxdj", "2");
        params.put("sfytb", "0");
        params.put("prename", "陈校长");
        params.put("pretel", "你猜135121212");
        params.put("schooljz", "16103");
    }

    //测试区县代码是否为空
    private void validateDistrictCode(Map<String, Object> params) {
        params.put("schoolcode", "3101160001");
        params.put("schoolname", "金山好好学习幼儿园");
        params.put("address", "金山区");
        params.put("zipcode", "200001");
        params.put("schoolywname", "ewrewe");
        params.put("orgid", "23423");
        params.put("faxnum", "031-1234");
        params.put("schooltelephone", "7711789");
        params.put("schoolurl", "http://www.baidu.com");
        params.put("districtcode", "");
        params.put("jxny", "20160606");
        params.put("xxlb", "1");
        params.put("xxbb", "1");
        params.put("bxdj", "4");
        params.put("sfytb", "1");
        params.put("prename", "陈校长");
        params.put("pretel", "135121212");
        params.put("schooljz", "16103");
    }

    //测试学校办别和办学等级的关系
    private void validateXxbb(Map<String, Object> params) {
        params.put("schoolcode", "3101160001");
        params.put("schoolname", "金山好好学习幼儿园");
        params.put("address", "金山区");
        params.put("zipcode", "200001");
        params.put("schoolywname", "werwer");
        params.put("orgid", "234234");
        params.put("faxnum", "031-1234");
        params.put("schooltelephone", "7711789");
        params.put("schoolurl", "http://www.baidu.com");
        params.put("districtcode", "310116");
        params.put("jxny", "201606");
        params.put("xxlb", "1");
        params.put("xxbb", "1");
        params.put("bxdj", "5");
        params.put("sfytb", "0");
        params.put("prename", "陈校长");
        params.put("pretel", "135121212");
        params.put("schooljz", "16103");
    }

    @Test
    public void testCancelSchool() {
        System.out.println("注销学校/删除学校");
        //封装参数
        request.setContent("{\"schoolcode\":\"0001\"}".getBytes());
        //注销学校
        JSONObject rs = schoolController.cancelSchool(request, response);
        String jiem = (String) rs.get("response");
        System.out.println(Encrypt.jiem(jiem));
    }

    @Test
    public void testRecoverSchool() {
        System.out.println("恢复学校");
        //封装参数
        request.setContent("{\"schoolcode\":\"0001\"}".getBytes());
        //注销学校
        JSONObject rs = schoolController.recoverSchool(request, response);
        String jiem = (String) rs.get("response");
        System.out.println(Encrypt.jiem(jiem));
    }

    @Test
    public void testModifySchool() {
        System.out.println("修改学校/学校修改");
        //组装参数
        Map<String,Object> params = new HashMap<>();
        validateSchoolCode3(params); //测试学校代码是否为空
//        validateSchoolCode1(params); //测试学校代码长度
//        validateSchoolCode2(params); //测试学校代码是否纯数字
//        validateOrgid2(params); //测试组织机构 是否为空
//        validateOrgid1(params); //测试组织结构 非(大写英文字母、数字、英文中划线)
//        validateSchoolYwName2(params); //测试学校英文名称是否为空
//        validateSchoolYwName1(params); //测试学校英文名称 非(大小写英文字母、间隔符号、空格、英文中划线
//        validateSchoolTel(params); //测试学校电话号码是否为数字
//        validateFaxNum(params); //测试学校传真是否为(数字、英文中划线)
//        validateZipCode1(params); //测试邮政编码长度是否为6位
//        validateZipCode2(params); //邮政编码是否为数字
//        validateAdminField(params); //测试学校行政字段是否有空
//        validtePreTel(params); //测试校长手机号码是否是数字
//        validateDistrictCode(params); //测试区县代码是否为空
//        validateXxbb(params); //测试学校办别和办学等级对应关系
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //修改学校
        JSONObject rs = schoolController.modifySchool(request, response);
        String jiem = (String) rs.get("response");
        System.out.println(Encrypt.jiem(jiem));
    }

    @Test
    public void testGetSchool() {
        System.out.println("查看学校/学校详情");
        //封装参数
        request.setContent("{\"schoolcode\": \"0001\"}".getBytes());
        //查看学校
        JSONObject rs = schoolController.getSchool(request, response);
        String jiem = (String) rs.get("response");
        System.out.println(Encrypt.jiem(jiem));

    }

    @Test
    public void testQuerySchool() {
        System.out.println("查询学校/学校列表");
        //封装参数
        request.setContent("{\"districtcode\": \"310116\"}".getBytes());
        //查询学校
        JSONObject rs = schoolController.querySchool(request, response);
        String jiem = (String) rs.get("response");
        System.out.println(Encrypt.jiem(jiem));
    }

    @Test
    public void testExpSchool() {
        System.out.println("导出学校/学校导出");
        //封装参数
        request.setContent("{\"districtcode\": \"310116\"}".getBytes());
        //导出学校
        JSONObject rs = schoolController.expSchool(request, response);
        String jiem = (String) rs.get("response");
        System.out.println(Encrypt.jiem(jiem));
    }

    @Test
    public void getSchoolByDistrict() {
        System.out.println("根据区县获取学校");
        //封装参数
        request.setContent("{\"districtcode\": \"310116\"}".getBytes());
        //获取学校下拉框
        JSONObject rs = schoolController.getSchoolByDistrict(request, response);
        String jiem = (String) rs.get("response");
        System.out.println(Encrypt.jiem(jiem));
    }

    @Test
    public void getQuerySchoolDistrict() {
        System.out.println("学校招生片区");
        Map<String,Object> params=new HashMap<>();
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //封装参数
        JSONObject rs = schoolController.querySchoolDistrict(request, response);
        String jiem = (String) rs.get("response");
        System.out.println(Encrypt.jiem(jiem));
    }


    //获取缓存LoginInfo
    private LoginInfo getLoginfo() {
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setId("测试123");
        loginInfo.setRoleid("3");
        loginInfo.setDistrictcode("310109");
        loginInfo.setRoletype("3");
        loginInfo.setSchoolcode("3101090001");
        loginInfo.setUsername("测试123");
        loginInfo.setRealname("junit测试管理员");
        return loginInfo;
    }
}
